//
//  CCYVisitView.swift
//  03.微博
//
//  Created by 蔡承毅 on 16/9/17.
//  Copyright © 2016年 IOS. All rights reserved.
//

import UIKit
protocol CCYVisitViewDelegate: NSObjectProtocol {
    func visitViewRegister()
    func visitViewLogin()
}

class CCYVisitView: UIView {
    
    
    
    
    // 添加代理属性
    weak var delegate: CCYVisitViewDelegate?
    
    required init?(coder aDecoder: NSCoder) {
     fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = UIColor.red
        setupUI()
    }
    
    // 私有的 setupUI方法
    private func setupUI(){
        
        // 背景颜色改变
        self.backgroundColor = UIColor(white: 237 / 255.0, alpha: 1)
        
        // 添加子控件
        self.addSubview(iconView)
        self.addSubview(coverView)
        self.addSubview(houseView)
        self.addSubview(messageLabel)
        self.addSubview(registerButton)
        self.addSubview(loginButton)
        
        // 添加约束
        iconView.translatesAutoresizingMaskIntoConstraints = false
        houseView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false
        
        // 转轮
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.centerX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.centerY, multiplier: 1, constant: -40))
        
        // 房子
        addConstraint(NSLayoutConstraint(item: houseView, attribute: .centerX, relatedBy: .equal, toItem: iconView, attribute: .centerX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: houseView, attribute: .centerY, relatedBy: .equal, toItem: iconView, attribute: .centerY, multiplier: 1, constant: 0))
        
        // 文字标签
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .centerX, relatedBy: .equal, toItem: iconView, attribute: .centerX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .top, relatedBy: .equal, toItem: iconView, attribute: .bottom, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 224))
        
        // 注册按钮
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .top, relatedBy: .equal, toItem: messageLabel, attribute: .bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .leading, relatedBy: .equal, toItem: messageLabel, attribute: .leading, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 35))
        
        // 登录按钮
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .top, relatedBy: .equal, toItem: messageLabel, attribute: .bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .trailing, relatedBy: .equal, toItem: messageLabel, attribute: .trailing, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 35))
        
        // 遮罩
        addConstraint(NSLayoutConstraint(item: coverView, attribute: .top, relatedBy: .equal, toItem: iconView, attribute: .top, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: .leading, relatedBy: .equal, toItem: iconView, attribute: .leading, multiplier: 1, constant: 0))
    }
    
    // MARK: - 懒加载
    // 转轮
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    // 小房子
    private lazy var houseView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    // 文字标签
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        
        label.font = UIFont.systemFont(ofSize: 16)
        label.textColor = UIColor.darkGray
        label.text = "关注一些人，看看有什么惊喜"
        label.numberOfLines = 0
        
        return label
    }()
    
    // 注册按钮
    private lazy var registerButton: UIButton = {
       let button = UIButton()
        
        // 设置按钮
        button.setBackgroundImage(UIImage(named:"common_button_white_disable"), for: .normal)
        button.setTitle("注册", for: .normal)
        button.setTitleColor(UIColor.orange, for: .normal)
        
        button.addTarget(self, action: #selector(registerDidClick), for: .touchUpInside)
        
        return button
    }()
    
    // 登录按钮
    private lazy var loginButton: UIButton = {
        let button = UIButton()
        
        button.setBackgroundImage(UIImage(named:"common_button_white_disable"), for: .normal)
        button.setTitle("登录", for: .normal)
        button.setTitleColor(UIColor.orange, for: .normal)
        
        button.addTarget(self, action: #selector(loginDidClick), for: .touchUpInside)
        
        return button
    }()
    
    // 遮罩
    lazy var coverView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    /// 不同页面设置不同信息
    func setupVisitorInfo(imageName: String , message: String){
        iconView.image = UIImage(named: imageName)
        
        messageLabel.text = message
        messageLabel.sizeToFit()
        
        houseView.isHidden = true
        
        self.sendSubview(toBack: coverView)
    }
    
    
    /// 旋转动画
    func startRotationAnimation() -> Void {
        let animation = CABasicAnimation(keyPath: "transform.rotation");
        animation.toValue = 2 * M_PI
        animation.duration = 20
        animation.repeatCount = MAXFLOAT
        
        animation.isRemovedOnCompletion = false
        
        iconView.layer.add(animation, forKey: "homeRotation")
        
    }
    
    func registerDidClick() {
        delegate?.visitViewRegister()
    }
    
    func loginDidClick() {
        delegate?.visitViewLogin()
    }
    
}
